package com.ymatou.iapi.pushbind.testcase;

import java.math.BigDecimal;
import java.util.Random;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.pushbind.parameter.BindUserClientBean;
import com.ymatou.iapi.pushbind.parameter.CleanRedisBindDataBean;
import com.ymatou.iapi.pushbind.parameter.Enum.UserSystemType;
import com.ymatou.iapi.pushbind.service.CleanRedisBindDataCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.PushCallService;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.utils.YMTDateUtil;

import net.sf.json.JSONObject;
import redis.clients.jedis.Tuple;
/**
*清理im的redis数据
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CleanRedisBindData{
	private static CleanRedisBindDataBean cleanredisbinddataBean;
	private static CleanRedisBindDataCall cleanredisbinddataCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("清理im的redis数据");
	}

	@Before
	public void caseUp() {
		cleanredisbinddataBean=new CleanRedisBindDataBean();
		cleanredisbinddataCall=new CleanRedisBindDataCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(cleanredisbinddataCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CleanRedisBindData_001() {
		Logger.start(true,"清理im的redis数据成功");
		try {
			//当前规则 YmatouUser 18个月前的记录 清理 最少一批1000个
			int userId = 1;
			String deviceId1 = "xiaomage123"+ YMTDateUtil.getBeforeOrNextSecond(-1);
			String clientId = "Test" + YMTDateUtil.getDate();
			BindUserClientBean binduserclientBean = PushCallService.intitUserClient(userId);
			binduserclientBean.setDeviceId(deviceId1);
			binduserclientBean.setClientId(clientId);
			//BindRecord:UserBindSet
			 binduserclientBean.setUserSystemType(UserSystemType.YmatouUser);
			PushCallService.bindUserClient(binduserclientBean);
			//BindRecord:UserBindSet:1
			 binduserclientBean.setUserSystemType(UserSystemType.CsUser);
			PushCallService.bindUserClient(binduserclientBean);
			//BindRecord:UserBindSet:2
			 binduserclientBean.setUserSystemType(UserSystemType.YmatouSellerCsUser);
			PushCallService.bindUserClient(binduserclientBean);
			Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			String key="BindRecord:UserBindSet:"+userId;
			String key1="BindRecord:UserBindSet:1:"+userId;
			String key2="BindRecord:UserBindSet:2:"+userId;
			Set<Tuple> set= im.getBindUserRedisWithScores(key);
			Set<Tuple> set1= im.getBindUserRedisWithScores(key1);
			Set<Tuple> set2= im.getBindUserRedisWithScores(key2);
			Logger.debug("key:"+key);
			for (Tuple s:set) {
				JSONObject json = FormatUtil.JSONToJSONObject(s.getElement());
				Logger.debug(BigDecimal.valueOf(s.getScore()).setScale(0).toString()+":"+s.getElement());
				//20200220112339860
				//yyyyMMddHHmmSS
				Double score=Double.valueOf(YMTDateUtil.getBeforeOrNextDay(-18*31, "yyyyMMddHHmmssSSS"));
				im.ZaddBindUserRedisWithScores(key, score, s.getElement());
			};
			Logger.debug("key:"+key1);
			for (Tuple s:set1) {
				JSONObject json = FormatUtil.JSONToJSONObject(s.getElement());
				Logger.debug(BigDecimal.valueOf(s.getScore()).setScale(0).toString()+":"+s.getElement());
				//20200220112339860
				//yyyyMMddHHmmSS
				Double score=Double.valueOf(YMTDateUtil.getBeforeOrNextDay(-18*31, "yyyyMMddHHmmssSSS"));
				im.ZaddBindUserRedisWithScores(key1, score, s.getElement());
			};
			Logger.debug("key:"+key2);
			for (Tuple s:set2) {
				JSONObject json = FormatUtil.JSONToJSONObject(s.getElement());
				Logger.debug(BigDecimal.valueOf(s.getScore()).setScale(0).toString()+":"+s.getElement());
				//20200220112339860
				//yyyyMMddHHmmSS
				Double score=Double.valueOf(YMTDateUtil.getBeforeOrNextDay(-18*31, "yyyyMMddHHmmssSSS"));
				im.ZaddBindUserRedisWithScores(key2, score, s.getElement());
			};
			String param="maxUserId=1000&beginUserId=0";
			cleanredisbinddataCall=new CleanRedisBindDataCall(param);
			cleanredisbinddataBean.setAppId("autotest");
			cleanredisbinddataCall.setData(cleanredisbinddataBean);
			cleanredisbinddataCall.callService();			
			Logger.verifyEquals("ok", cleanredisbinddataCall.getReturnData(), "验证返回");
			Thread.sleep(30000);
			
			Logger.debug("检查清理完成");
			set= im.getBindUserRedisWithScores(key);
			for (Tuple s:set) {
				JSONObject json = FormatUtil.JSONToJSONObject(s.getElement());
				Logger.debug(BigDecimal.valueOf(s.getScore()).setScale(0).toString()+":"+s.getElement());
			};
			Logger.verifyEquals(0, set.size(), "验证数据被清理完成:"+key);
			set1= im.getBindUserRedisWithScores(key1);
			for (Tuple s:set1) {
				JSONObject json = FormatUtil.JSONToJSONObject(s.getElement());
				Logger.debug(BigDecimal.valueOf(s.getScore()).setScale(0).toString()+":"+s.getElement());
			};
			Logger.verifyEquals(0, set1.size(), "验证数据被清理完成:"+key1);
			set2= im.getBindUserRedisWithScores(key2);
			for (Tuple s:set2) {
				JSONObject json = FormatUtil.JSONToJSONObject(s.getElement());
				Logger.debug(BigDecimal.valueOf(s.getScore()).setScale(0).toString()+":"+s.getElement());
			};
			Logger.verifyEquals(0, set2.size(), "验证数据没有清理完成:"+key2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}